//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.utils.indexer;

import ants.p2p.query.*;
import ants.p2p.utils.indexer.*;
import java.io.*;
import java.util.*;

public class RemoteFileInfos implements Serializable{
  String hash;
  String ed2kFileHash;
  String name;
  String extendedInfos;
  Hashtable owners = new Hashtable();
  long size;
  private final boolean marker = false;

  public RemoteFileInfos(QueryFileTuple qft){
    long lastTimeSeen = 0;
    if(!(qft instanceof QueryRemoteFileTuple)){
      lastTimeSeen = System.currentTimeMillis();
      this.extendedInfos = qft.getExtendedInfos();
    }else{
      QueryRemoteFileTuple qrft = (QueryRemoteFileTuple)qft;
      lastTimeSeen = qrft.getLastTimeSeen().longValue();
      this.extendedInfos = qft.getExtendedInfos();
    }
    this.owners.put(qft.getOwnerID(), new Long(lastTimeSeen));
    this.hash = qft.getFileHash();
    this.ed2kFileHash = qft.getED2KFileHash();
    this.name = qft.getFileName();
    this.size = qft.getSize().longValue();
  }

  public String getName(){
    return this.name;
  }

  public String getHash(){
    return this.hash;
  }

  public Hashtable getOwners(){
    return this.owners;
  }

  public boolean hasSources(long timeOut){
    Enumeration sources = this.owners.keys();
    while(sources.hasMoreElements()){
      String sourceHash = (String)sources.nextElement();
      Long sourceLastTimeSeen = (Long)this.owners.get(sourceHash);
      if(System.currentTimeMillis() - sourceLastTimeSeen.longValue() > timeOut){
        this.owners.remove(sourceHash);
      }
    }
    if(this.owners.size() == 0)
      return false;
    else
      return true;
  }

  public String getED2KFileHash(){
    return this.ed2kFileHash;
  }

  public long getSize() {
    return this.size;
  }

  public boolean equals(Object o){
    if(o instanceof RemoteFileInfos)
      return ((RemoteFileInfos)o).hash.equals(this.hash);
    else if(o instanceof FileInfos)
      return ((FileInfos)o).getHash().equals(this.hash);
    else if(o instanceof String)
      return ((String)o).equals(this.hash);
    else
      return this==o;
  }

  public String getExtendedInfos(){
    return this.extendedInfos;
  }
}
